package cn.yfj.org.controller;



import cn.yfj.basic.util.AjaxResult;
import cn.yfj.basic.util.PageList;
import cn.yfj.org.domain.Employee;
import cn.yfj.org.query.EmployeeQuery;
import cn.yfj.org.service.IEmployeeServiceI;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController// = @Controller + @ResponseBody
@RequestMapping("/employee")// http://localhost:8080/dept
//@GetMapping =@RequestMapping(value = "/id",method = RequestMethod.GET)
//@PostMapping
//@PutMapping
//@PatchMapping  批量
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {
    @Autowired
    private IEmployeeServiceI employeeService;

    //新增和修改   put
    @PutMapping// http://localhost:8080/dept/2
    @ApiOperation(value = "新增或修改，如果传递ID=修改，否则新增" )
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId() == null){
                employeeService.save(employee);
            }else{
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("噢哟，系统报错了，我们正在殴打程序员！！");
        }
    }

    // 删除      delete
    public AjaxResult remove(@PathVariable("id") Long id){

        try {
            employeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("噢哟，系统报错了，我们正在殴打程序员！！");
        }
    }

    @PatchMapping
    public AjaxResult batch(@RequestBody List<Long> ids){
        try {
            employeeService.bach(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("噢哟，系统报错了，我们正在殴打程序员！！");
        }
    }


    //获取一条记录  get  /1
    @GetMapping("/{id}")
    public Employee queryById(@PathVariable("id") Long id){
        return employeeService.queryById(id);
    }

    //获取所有数据  get
    @GetMapping
    public List<Employee> queryAll(){
        return employeeService.queryAll();
    }

    //分页 + 高级查询  post
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        return employeeService.queryPage(query);
    }

}
